/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Reports.Matcher;

import ResultEditor.Annotations.Annotation;
import ResultEditor.Annotations.Article;
import ResultEditor.Annotations.Depot;
import java.util.Vector;

/**
 * @author leng
 */
public class AnnotatorRegister {
    
    /***/
    private static Vector<String> annotators = new Vector<String>();

    /**remove vector of "annotators". */
    private void clear(){
        annotators.clear();
    }

    public String getAnnotatorByIndex(int index){
        if( (index<0) || (index>(size()-1)) )
            return null;
        return annotators.get(index);
    }

    public int size(){
        return annotators.size();
    }

    public int register(){
        try{
            // ##1## clear memory
            clear();

            // ##2## get all annotation
            Depot depot = new Depot();
            final Vector<Article> anns = depot.getDepot();

            if(anns==null)
                return 0;

            // ##3## go over all articles
            for( Article article : anns ){
                if ( article == null )
                    continue;
                if ( article.annotations == null )
                    continue;

                // ##4## go over all annotaitons to each article
                for( Annotation ann : article.annotations ){
                    if( ann == null )
                        continue;
                    String annotator = ann.annotator;
                    add(annotator);
                }
            }

            // ##4## end
            return annotators.size();
            
        }catch(Exception ex){
            System.out.println("error occurred while");
            return annotators.size();
        }
    }

    /**Record the annotator into static memory of "annotators" in class
     * "Reports.Matcher.AnnotatorRegister"
     *
     * @param   annotator_name
     *          String type value describes the name of the annotator.
     */
    private void add(String annotator_name){
        if( !isExisting(annotator_name) ){
            annotators.add(annotator_name);
        }
    }

    private boolean isExisting(String annotator_name){
        if(annotator_name==null)
            return true;
        if(annotator_name.trim().length()<1)
            return true;
        
        if(annotators.size() < 1){
            return false;
        }

        for(String annotator: annotators){
            if(annotator_name.trim().compareTo(annotator.trim())==0)
                return true;
        }

        return false;
    }
}
